package com.youkeyi.ddy.cloud.basic.storage.stock.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.storage.stock.entity.StoStockOptionLog;
import com.youkeyi.ddy.cloud.basic.storage.stock.service.StoStockOptionLogService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.StorageStockOptionLogCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.dto.StorageStockOptionLogListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.dto.StorageStockOptionLogListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.stock.param.*;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 库存操作日志 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-03-26
 */
@Service
public class StoStockOptionLogController implements StorageStockOptionLogCloudService {

    @Autowired
    private StoStockOptionLogService stoStockOptionLogService;

    @Override
    public StorageStockOptionLogListCloudDto query(StorageStockOptionLogQueryCloudParam param) {
        StorageStockOptionLogListCloudDto dto = new StorageStockOptionLogListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getStockOptionLogPrimaryKeys())) {
            param.getStockOptionLogPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getBillNos())) {
            param.getBillNos().removeAll(Collections.singleton(null));
        }

        QueryWrapper<StoStockOptionLog> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getStockOptionLogPrimaryKeys()), StoStockOptionLog::getPrimaryKey, param.getStockOptionLogPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), StoStockOptionLog::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getBillNos()), StoStockOptionLog::getBillNo, param.getBillNos())
                .like(StringUtils.isNotBlank(param.getKeyWordFuzzy()), StoStockOptionLog::getKeyWord, param.getKeyWordFuzzy())
        ;

        Page<StoStockOptionLog> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<StoStockOptionLog> stockOptionLogList = stoStockOptionLogService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(stockOptionLogList.getRecords())) {
            dto.setTotal(stockOptionLogList.getTotal());

            List<StorageStockOptionLogListItemCloudDto> items = new ArrayList<>();
            stockOptionLogList.getRecords().forEach(stockOptionLog -> {
                StorageStockOptionLogListItemCloudDto item = new StorageStockOptionLogListItemCloudDto();
                item.setStockOptionLogPrimaryKey(stockOptionLog.getPrimaryKey());
                item.setCorpKey(stockOptionLog.getCorpKey());
                item.setBillNo(stockOptionLog.getBillNo());
                item.setWarehouseKey(stockOptionLog.getWarehouseKey());
                item.setWarehouseName(stockOptionLog.getWarehouseName());
                item.setBarCode(stockOptionLog.getBarCode());
                item.setGoodsKey(stockOptionLog.getGoodsKey());
                item.setGoodsName(stockOptionLog.getGoodsName());
                item.setKeyWord(stockOptionLog.getKeyWord());
                item.setUnitKey(stockOptionLog.getUnitKey());
                item.setUnitName(stockOptionLog.getUnitName());
                item.setProductDate(stockOptionLog.getProductDate() != null ? stockOptionLog.getProductDate().getTime() : null);
                item.setExpirationDate(stockOptionLog.getExpirationDate() != null ? stockOptionLog.getExpirationDate().getTime() : null);
                item.setShelfKey(stockOptionLog.getShelfKey());
                item.setQuantity(stockOptionLog.getQuantity());
                item.setPrice(stockOptionLog.getPrice());
                item.setSupplierKey(stockOptionLog.getSupplierKey());
                item.setSupplierName(stockOptionLog.getSupplierName());
                item.setAdvicePrice(stockOptionLog.getAdvicePrice());
                item.setInStoreType(stockOptionLog.getInStoreType());
                item.setState(stockOptionLog.getState());
                item.setOrigStockKey(stockOptionLog.getOrigStockKey());
                item.setOptionUserKey(stockOptionLog.getOptionUserKey());
                item.setInsertType(stockOptionLog.getInsertType());
                item.setPreState(stockOptionLog.getPreState());
                item.setPreQuantity(stockOptionLog.getPreQuantity());
                item.setPrePrice(stockOptionLog.getPrePrice());
                item.setCooperationMode(stockOptionLog.getCooperationMode());
                item.setPurchaseUserKey(stockOptionLog.getPurchaseUserKey());
                item.setStorageUserKey(stockOptionLog.getStorageUserKey());
                item.setStorageTime(stockOptionLog.getStorageTime() != null ? stockOptionLog.getStorageTime().getTime() : null);
                item.setPayType(stockOptionLog.getPayType());
                item.setNotes(stockOptionLog.getNotes());
                item.setFromType(stockOptionLog.getFromType());
                item.setCreateTime(stockOptionLog.getCreateTime() != null ? stockOptionLog.getCreateTime().getTime() : null);
                item.setCreateUserKey(stockOptionLog.getCreateUserKey());
                item.setCreateUserName(stockOptionLog.getCreateUserName());
                item.setUpdateTime(stockOptionLog.getUpdateTime() != null ? stockOptionLog.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(stockOptionLog.getUpdateUserKey());
                item.setUpdateUserName(stockOptionLog.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(StorageStockOptionLogCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getGoodsKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        StoStockOptionLog stockOptionLog = new StoStockOptionLog();
        stockOptionLog.setPrimaryKey(SimpleUUidUtils.getUuid());
        stockOptionLog.setCorpKey(param.getCorpKey());
        stockOptionLog.setBillNo(param.getBillNo());
        stockOptionLog.setWarehouseKey(param.getWarehouseKey());
        stockOptionLog.setWarehouseName(param.getWarehouseName());
        stockOptionLog.setBarCode(param.getBarCode());
        stockOptionLog.setGoodsKey(param.getGoodsKey());
        stockOptionLog.setGoodsName(param.getGoodsName());
        stockOptionLog.setKeyWord(param.getKeyWord());
        stockOptionLog.setUnitKey(param.getUnitKey());
        stockOptionLog.setUnitName(param.getUnitName());
        stockOptionLog.setProductDate(param.getProductDate() != null ? new Date(param.getProductDate()) : null);
        stockOptionLog.setExpirationDate(param.getExpirationDate() != null ? new Date(param.getExpirationDate()) : null);
        stockOptionLog.setShelfKey(param.getShelfKey());
        stockOptionLog.setQuantity(param.getQuantity());
        stockOptionLog.setPrice(param.getPrice());
        stockOptionLog.setAdvicePrice(param.getAdvicePrice());
        stockOptionLog.setInStoreType(param.getInStoreType());
        stockOptionLog.setState(param.getState());
        stockOptionLog.setOrigStockKey(param.getOrigStockKey());
        stockOptionLog.setOptionUserKey(param.getOptionUserKey());
        stockOptionLog.setInsertType(param.getInsertType());
        stockOptionLog.setPreState(param.getPreState());
        stockOptionLog.setPreQuantity(param.getPreQuantity());
        stockOptionLog.setPrePrice(param.getPrePrice());
        stockOptionLog.setCooperationMode(param.getCooperationMode());
        stockOptionLog.setPurchaseUserKey(param.getPurchaseUserKey());
        stockOptionLog.setStorageUserKey(param.getStorageUserKey());
        stockOptionLog.setStorageTime(param.getStorageTime() != null ? new Date(param.getStorageTime()) : null);
        stockOptionLog.setPayType(param.getPayType());
        stockOptionLog.setSupplierKey(param.getSupplierKey());
        stockOptionLog.setSupplierName(param.getSupplierName());
        stockOptionLog.setNotes(param.getNotes());
        stockOptionLog.setFromType(param.getFromType());
        stockOptionLog.setCreateUserKey(param.getCreateUserKey());
        stockOptionLog.setCreateUserName(param.getCreateUserName());
        stockOptionLog.setCreateTime(new Date());
        boolean save = stoStockOptionLogService.save(stockOptionLog);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(stockOptionLog.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(StorageStockOptionLogUpdateCloudParam param) {
        return null;
    }

    @Override
    public ResponseContent delete(StorageStockOptionLogDeleteCloudParam param) {
        return null;
    }
}
